package zw_401_500.zw_434_字符串中的单词数;

class Solution {

    public static void main(String[] args) {
        String s = "Hello, my name is John";
        int count = countSegments(s);
        System.out.println(count);
    }

    public static int countSegments(String s) {
        int segmentCount = 0;

        for (int i = 0; i < s.length(); i++) {
            if ((i == 0 || s.charAt(i - 1) == ' ') && s.charAt(i) != ' ') {
                segmentCount++;
            }
        }

        return segmentCount;
    }
}
